﻿namespace BM.Tools.WebControls.MForm.Controls.ValueHandlers
{
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using BM.Tools.WebControls.MForm.Controls.Helper;
    using BM.Tools.WebControls.MForm.Controls.Items;

    using Generator;

    /// <summary>
    /// The standard web control value handler binds the <see cref="LeafItem" /> control with a single web control that is inside the value handler
    /// </summary>
    /// <typeparam name="T">
    /// the type of the web control 
    /// </typeparam>
    public abstract class StandardWebControlValueHandler<T> : ControlValueHandler<T>, IWebControlValueHandler
            where T : WebControl, new()
    {        
        /// <summary>
        /// Gets or sets Value.
        /// </summary>
        public override string Value
        {
            get
            {                
                var textControl = Control as ITextControl;
                return textControl != null ? textControl.Text : null;
            }

            set
            {
                var textControl = Control as ITextControl;
                if (textControl != null)
                {
                    textControl.Text = value;
                }
            }
        }

        /// <summary>
        /// Gets WebControl.
        /// </summary>
        /// <value>
        /// The web control.
        /// </value>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public WebControl WebControl
        {
            get
            {
                return (Owner.Mode == FormBuilderMode.View) ? ViewModeControl : EditModeControl;
            }
        }

        /// <summary>
        /// Gets the web control that is used in <see cref="FormBuilderMode.Edit"/>
        /// </summary>
        public WebControl EditModeControl
        {
            get
            {
                return Control;
            }
        }

        /// <summary>
        /// Gets the web control that is used in <see cref="FormBuilderMode.View"/>
        /// </summary>
        WebControl IWebControlValueHandler.ViewModeControl
        {
            get
            {
                return ViewModeControl;                
            }        
        }

        /// <summary>
        /// Prepares the control and its css class
        /// </summary>
        protected override void PrepareControl()
        {
            base.PrepareControl();
            PrepareCssClass();
        }
        
        /// <summary>
        /// Prepares the css class of the web control
        /// </summary>
        /// <returns>
        /// the CssClasses collection
        /// </returns>
        protected virtual CssClasses PrepareCssClass()
        {
            CssClasses cssClasses = new CssClasses(WebControl);
            cssClasses.Add(LeafItem.InputCssClass);
            return cssClasses;
        }
    }
}